<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用传递的数组递归替换第一个数组的元素</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-replace-recursive" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_replace_recursive</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7)</p><p class="refpurpose"><span class="refname">array_replace_recursive</span> &mdash; <span class="dc-title">使用传递的数组递归替换第一个数组的元素</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-replace-recursive-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_replace_recursive</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array1</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
  ] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_replace_recursive()</strong></span> 使用后面数组元素的值替换数组 <code class="parameter">array1</code> 的值。
   如果一个键存在于第一个数组同时也存在于第二个数组，它的值将被第二个数组中的值替换。
   如果一个键存在于第二个数组，但是不存在于第一个数组，则会在第一个数组中创建这个元素。
   如果一个键仅存在于第一个数组，它将保持不变。
  如果传递了多个替换数组，它们将被按顺序依次处理，后面的数组将覆盖之前的值。
  </p>
  <p class="para"> <span class="function"><strong>array_replace_recursive()</strong></span> 是递归的：它将遍历数组并将相同的处理应用到数组的内部值。
  </p>
  <p class="para">
   如果第一个数组中的值是标量，它的值将被第二个数组中的值替换，它可能是一个标量或者数组。如果第一个数组和第二个数组中的值都是数组，<span class="function"><strong>array_replace_recursive()</strong></span> 函数将递归地替换它们各自的值。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-replace-recursive-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array1</code></dt>

     <dd>

      <p class="para">
       替换该数组的值。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

      <p class="para">
       可选项。包含要提取元素的数组。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-replace-recursive-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个<span class="type"><span class="type 数组">数组</span></span>。如果发生错误，将返回 <strong><code>NULL</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-replace-recursive-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6060">
    <p><strong>Example #1 <span class="function"><strong>array_replace_recursive()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$base&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'citrus'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">'berries'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"blackberry"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"raspberry"</span><span style="color: #007700">),&nbsp;);<br /></span><span style="color: #0000BB">$replacements&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'citrus'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'pineapple'</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">'berries'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'blueberry'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$basket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_replace_recursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replacements</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$basket</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$basket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replacements</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$basket</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [citrus] =&gt; Array
        (
            [0] =&gt; pineapple
        )

    [berries] =&gt; Array
        (
            [0] =&gt; blueberry
            [1] =&gt; raspberry
        )

)
Array
(
    [citrus] =&gt; Array
        (
            [0] =&gt; pineapple
        )

    [berries] =&gt; Array
        (
            [0] =&gt; blueberry
        )

)

</pre></div>
    </div>
   </div>
   <div class="example" id="example-6061">
    <p><strong>Example #2 <span class="function"><strong>array_replace_recursive()</strong></span> 及其递归表现</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$base&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'citrus'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">'berries'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"blackberry"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"raspberry"</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">'others'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'banana'&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$replacements&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'citrus'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'pineapple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'berries'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'blueberry'</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">'others'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'litchis'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$replacements2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'citrus'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'pineapple'</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">'berries'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'blueberry'</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">'others'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'litchis'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$basket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_replace_recursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replacements</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replacements2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$basket</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [citrus] =&gt; Array
        (
            [0] =&gt; pineapple
        )

    [berries] =&gt; Array
        (
            [0] =&gt; blueberry
            [1] =&gt; raspberry
        )

    [others] =&gt; litchis
)
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-replace-recursive-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_replace.html" class="function" rel="rdfs-seeAlso">array_replace()</a> - 使用传递的数组替换第一个数组的元素</span></li>
    <li class="member"><span class="function"><a href="array_merge_recursive.html" class="function" rel="rdfs-seeAlso">array_merge_recursive()</a> - 递归地合并一个或多个数组</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>